package com.jstyle.jclifexd.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Teamperformance;
import com.jstyle.jclifexd.utils.DateUtil;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.StringTokenizer;
import butterknife.BindView;
import butterknife.ButterKnife;


public class TeamperdataAdapter extends RecyclerView.Adapter{
    private List<Teamperformance> mData;
    private Context context;
    public TeamperdataAdapter(Context mycontext) {
        this.context =mycontext;
    }
    public  void  SettingData(List<Teamperformance> mymData){
        this.mData =mymData;
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(0==viewType){
            view=   LayoutInflater.from(context).inflate(R.layout.item_teamdata,  parent,false);
            return   new ExerciseRecor1(view);
        }else{
            view=   LayoutInflater.from(context).inflate(R.layout.item_teamdataer,  parent,false);
            return   new ExerciseRecor2(view);
        }
    }
    public class ExerciseRecor1 extends BaseViewHolder {//其他剩下的
        @BindView(R.id.title_name)
        TextView ti;
        @BindView(R.id.others)
        TextView t2;
        @BindView(R.id.otherer)
        TextView t3;
        @BindView(R.id.othersan)
        TextView t4;
        public ExerciseRecor1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public class ExerciseRecor2 extends BaseViewHolder {//其他剩下的
        @BindView(R.id.title_name)
        TextView ti;
        @BindView(R.id.others)
        TextView t2;
        @BindView(R.id.otherer)
        TextView t3;
        @BindView(R.id.othersan)
        TextView t4;
        @BindView(R.id.item_lineer)
        View view;
        public ExerciseRecor2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
        if(null==mData){return;}
      if(holder instanceof ExerciseRecor1){
          ExerciseRecor1 normalViewHolder=(ExerciseRecor1)holder;
          normalViewHolder. t2.setTextColor(Color.parseColor("#003D5C"));
          normalViewHolder.t3.setTextColor(Color.parseColor("#003D5C"));
          normalViewHolder.t4.setTextColor(Color.parseColor("#003D5C"));
          normalViewHolder.ti.setText("");
          if (mData.size()>=3){
              normalViewHolder.t2.setText(new StringTokenizer(mData.get(0).getTeamName(), "\\n").nextToken());
              normalViewHolder.t3.setText(new StringTokenizer(mData.get(1).getTeamName(), "\\n").nextToken());
              normalViewHolder. t4.setText(new StringTokenizer(mData.get(2).getTeamName(), "\\n").nextToken());
          }else if(mData.size()>=2){
              normalViewHolder.t2.setText(new StringTokenizer(mData.get(0).getTeamName(), "\\n").nextToken());
              normalViewHolder.t3.setText(new StringTokenizer(mData.get(1).getTeamName(), "\\n").nextToken());
          }else{
              normalViewHolder.t2.setText(new StringTokenizer(mData.get(0).getTeamName(), "\\n").nextToken());
          }
          if (mData.size()==2){
              normalViewHolder.t4.setVisibility(View.GONE);
          }else if(mData.size()==1){
              normalViewHolder.t3.setVisibility(View.GONE);
              normalViewHolder.t4.setVisibility(View.GONE);
          }
      }else{
          ExerciseRecor2 normalVi=(ExerciseRecor2)holder;
          if (mData.size()==2){
              normalVi.t4.setVisibility(View.GONE);
          }else if(mData.size()==1){
              normalVi.t3.setVisibility(View.GONE);
              normalVi.t4.setVisibility(View.GONE);
          }
          switch (position){
              case 1:
                  normalVi.ti.setText(context.getResources().getString(R.string.targetnumber));
                  normalVi.t2.setText(mData.get(0).getStandard()+context.getResources().getString(R.string.Times));
                  if(mData.size()>=2){ normalVi.t3.setText(mData.get(1).getStandard()+context.getResources().getString(R.string.Times)); }
                  if(mData.size()>=3){ normalVi.t4.setText(mData.get(2).getStandard()+context.getResources().getString(R.string.Times)); }
                  break;
              case 2:
                  normalVi.ti.setText(context.getResources().getString(R.string.averageduration));
                  normalVi.t2.setText(DateUtil.getSecond(Long.valueOf(mData.get(0).getAvgDuration())));
                  if(mData.size()>=2){ normalVi.t3.setText(DateUtil.getSecond(Long.valueOf(mData.get(1).getAvgDuration()))); }
                  if(mData.size()>=3){ normalVi.t4.setText(DateUtil.getSecond(Long.valueOf(mData.get(2).getAvgDuration())));}
                  break;
              case 3:
                  DecimalFormat df = new DecimalFormat("0.00");
                  normalVi.ti.setText(context.getResources().getString(R.string.meandistance));
                  normalVi.t2.setText((mData.get(0).getAvgDistance().contains(".")?df.format(new BigDecimal(mData.get(0).getAvgDistance())):mData.get(0).getAvgDistance())+context.getResources().getString(R.string.Kilometre));
                  if(mData.size()>=2){ normalVi.t3.setText((mData.get(1).getAvgDistance().contains(".")?df.format(new BigDecimal(mData.get(1).getAvgDistance())):mData.get(1).getAvgDistance())+context.getResources().getString(R.string.Kilometre)); }
                  if(mData.size()>=3){ normalVi.t4.setText((mData.get(2).getAvgDistance().contains(".")?df.format(new BigDecimal(mData.get(2).getAvgDistance())):mData.get(2).getAvgDistance())+context.getResources().getString(R.string.Kilometre));}
                  break;
              case 4:
                  normalVi.ti.setText(context.getResources().getString(R.string.avgStride) );
                  normalVi.t2.setText(mData.get(0).getAvgStride()+context.getResources().getString(R.string.Stride_label));
                  if(mData.size()>=2){ normalVi.t3.setText(mData.get(1).getAvgStride()+context.getResources().getString(R.string.Stride_label)); }
                  if(mData.size()>=3){ normalVi.t4.setText(mData.get(2).getAvgStride()+context.getResources().getString(R.string.Stride_label));}
                  break;
              case 5:
                  normalVi.ti.setText(context.getResources().getString(R.string.avgsep));
                  normalVi.t2.setText(mData.get(0).getAvgStep()+context.getResources().getString(R.string.Steps_unit));
                  if(mData.size()>=2){ normalVi.t3.setText(mData.get(1).getAvgStep()+context.getResources().getString(R.string.Steps_unit)); }
                  if(mData.size()>=3){ normalVi.t4.setText(mData.get(2).getAvgStep()+context.getResources().getString(R.string.Steps_unit));}
                  break;
              case 6:
                  normalVi.ti.setText(context.getResources().getString(R.string.fastestPace));
                  normalVi.t2.setText(FastestPace(mData.get(0).getFastestPace()));
                  if(mData.size()>=2){ normalVi.t3.setText(FastestPace(mData.get(1).getFastestPace())); }
                  if(mData.size()>=3){ normalVi.t4.setText(FastestPace(mData.get(2).getFastestPace()));}
                  break;
              case 7:
                  normalVi.ti.setText(context.getResources().getString(R.string.avgPace));
                  normalVi.t2.setText(AvgPace(mData.get(0).getAvgPace()));
                  if(mData.size()>=2){ normalVi.t3.setText(AvgPace(mData.get(1).getAvgPace())); }
                  if(mData.size()>=3){ normalVi.t4.setText(AvgPace(mData.get(2).getAvgPace()));}
                  break;

          }


      }
        Log.e("asdasdada",position+"");


    }


    /**
     * 计算最快配速
     * @param VALUE
     * @return
     */
    private String FastestPace(String VALUE){
        return  (Integer.valueOf(VALUE)/60)+"’"+(Integer.valueOf(VALUE)%60)+"”";
    }
    /**
     * 计算平均配速
     * @param VALUE
     * @return
     */
    private String AvgPace(String VALUE){
        return  (Integer.valueOf(VALUE)%256)+"’"+(Integer.valueOf(VALUE)/256)+"”";
    }



    @Override
    public int getItemCount() {
        return mData==null?0:8;
    }


}
